//
//  PopoverPresentationController.swift
//  swiftWeiBoDemo
//
//  Created by zzw on 16/5/6.
//  Copyright © 2016年 zzw. All rights reserved.
//

import UIKit

class PopoverPresentationController: UIPresentationController {
    
    /// 定义属性保存菜单的大小
    var presentFrame = CGRectZero
    
    /**
     初始化方法, 用于创建负责转场动画的对象
     
     :param: presentedViewController  被展现的控制器
     :param: presentingViewController 发起的控制器, Xocde6是nil, Xcode7是野指针
     
     :returns: 负责转场动画的对象
     */
    
    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
    }
    
    override func containerViewWillLayoutSubviews() {
        if  presentFrame == CGRectZero {
            presentedView()?.frame = CGRectMake(100, 56, 200, 200)
            
        }else{
        
            presentedView()?.frame = presentFrame
        }
        
       containerView?.insertSubview(coverView, atIndex: 0)
    }
    
    
    private lazy var coverView:UIView = {
    
        let v = UIView()
        v.backgroundColor = UIColor(white:0.0, alpha:0.2)
        
        v.frame = UIScreen.mainScreen().bounds
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(PopoverPresentationController.close))
        v.addGestureRecognizer(tap)
        return v
    
    }()
    
    func close(){
        
            presentedViewController.dismissViewControllerAnimated(true, completion: nil)
        
    }

}
